package com.vvlive.caibao.smartcity.ui;

import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.vvlive.caibao.smartcity.adapter.NotificationRecyclerAdapter;
import com.vvlive.caibao.smartcity.base.BaseActivity;
import com.vvlive.caibao.smartcity.bean.Notification;
import com.vvlive.caibao.smartcity.view.CommonActionBar;

import org.kymjs.kjframe.ui.BindView;

import java.util.ArrayList;
import java.util.List;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by zhj on 2015/7/16.
 * 通知页面
 * <p>
 * 微信消息
 * 活动消息
 * 考勤消息
 * 生活消息（订餐，定泳馆，定会展）
 */
public class NotificationActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener{

    @BindView(id = R.id.notification_recycler)
    RecyclerView notificationRecycler;
    @BindView(id = R.id.noti_refresh)
    SwipeRefreshLayout mRefresh;

    private List<Notification> mNotis = new ArrayList<>();
    private NotificationRecyclerAdapter mAdapter;

    /**
     * 点击通知要跳转的页面
     */
//    private Class<?>[] classes = {EnterpriseContactsActivity.class, MyOrdersActivity.class, CheckOnActivity.class, LifeOrderMealActivity.class};

    @Override
    protected int getContentViewRes() {
        return R.layout.activity_notification;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        CommonActionBar actionBar = getCommonActionBar();
        actionBar.setTitle("通知");
        actionBar.setMoreVisible(false);
        mNotis.addAll(getNotifications());
        mAdapter = new NotificationRecyclerAdapter(this, mNotis);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        notificationRecycler.setLayoutManager(layoutManager);
        notificationRecycler.setItemAnimator(new DefaultItemAnimator());
        notificationRecycler.setHasFixedSize(true);
        notificationRecycler.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new NotificationRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {

                // 点击通知跳转到相应的页面
//                newActivity(classes[position]);
            }
        });

        mRefresh.setOnRefreshListener(this);
        mRefresh.setColorSchemeColors(Color.RED);
    }

    /**
     * 获得所有的通知消息
     *
     * @return 通知消息
     */
    private List<Notification> getNotifications() {
        List<Notification> notis = new ArrayList<>();
        notis.add(new Notification("http://img0.imgtn.bdimg.com/it/u=4267046075,1536034367&fm=21&gp=0.jpg", "3条未读微信消息", "张三：你吃了吗", 3));
        notis.add(new Notification("http://img0.imgtn.bdimg.com/it/u=1741297299,240227733&fm=21&gp=0.jpg", "10条会议消息", "新的会议", 10));
        notis.add(new Notification("http://img0.imgtn.bdimg.com/it/u=3493113640,4182599844&fm=21&gp=0.jpg", "1条考勤消息", "补充迟到理由", 1));
        notis.add(new Notification("http://img2.imgtn.bdimg.com/it/u=648783313,939768751&fm=21&gp=0.jpg", "100条订餐消息", "满汉全席已经送到，请开门", 100));
        return notis;
    }

    @Override
    public void onRefresh() {
        mHandler.sendEmptyMessageDelayed(1, 2000);
    }

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == 1){
                mRefresh.setRefreshing(false);
                mNotis.addAll(getNotifications());
                mAdapter.notifyDataSetChanged();
            }
        }
    };
}
